<template>
  <el-container>
    <el-header>
      <h3>文章管理系统</h3>
      <img :src="imgUrl" alt="头像">
      <el-menu
        :default-active="activeIndex2"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        background-color="#242730"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <el-submenu index="1">
          <template slot="title">个人中心</template>
          <el-menu-item index="/changeUserMsg">基本信息</el-menu-item>
          <el-menu-item index="/changeUserPic">更换头像</el-menu-item>
          <el-menu-item index="/changeUserPassword">重置密码</el-menu-item>
        </el-submenu>
      </el-menu>
      <div class="signOut" @click="signOut">
        <i class="el-icon-switch-button"></i>
        <span>退出</span>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <aside-nav/>
      </el-aside>
      <el-container>
        <el-main>
<!--          二级路由-->
          <router-view></router-view>
        </el-main>
        <el-divider></el-divider>
        <el-footer>
          <span>
            张吃饭的小屋
          </span>
        </el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import pubsub from 'pubsub-js'
import { userInfo } from '@/api'
import asideNav from '../../components/asidenav'

export default {
  name: 'user-menu',
  data () {
    return {
      activeIndex: '1',
      activeIndex2: '1',
      imgUrl: require('../../assets/images/avatar.jpg'),
      userMsg: {}
    }
  },
  methods: {
    async getMsg () {
      const res = await userInfo()
      this.$store.commit('SAVEUSERMSG', res.data.data)
      this.$message.success(`欢迎${res.data.data.nickname}`)
      this.userMsg = this.$store.state.userInfo
      this.imgUrl = this.userMsg.user_pic ? this.userMsg.user_pic : require('../../assets/images/avatar.jpg')
    },
    handleSelect (key, keyPath) {
    },
    // 退出功能实现
    signOut () {
      this.$confirm('确认退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
        this.$store.commit('SAVETOKEN', '')
        localStorage.removeItem('token')
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  mounted () {
    this.getMsg()
    pubsub.subscribe('changePic', (_, data) => {
      this.imgUrl = data
    })
  },
  components: { asideNav }
}
</script>

<style scoped>
*{
  margin: 0;
  padding: 0;
}
.el-container{
  width: 100%;
  height: 100%;
}
.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}
.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
body > .el-container {
  margin-bottom: 40px;
}
.el-header{
  background-color: #242730;
  color: #333;
  text-align: center;
  line-height: 60px;
  position: relative;
}
.el-header h3{
  color: white;
  position: absolute;
  left: 40px;
}
.el-menu{
  width: 120px;
  position: absolute;
  right: 250px;
}
.el-header img{
  width: 50px;
  height: 50px;
  position: absolute;
  top: 6px;
  border-radius: 50%;
  right: 400px;
}
.signOut{
  position: absolute;
  right: 180px;
}
.signOut i{
  color: #fff;
}
.signOut span{
  color: #fff;
  margin-left: 10px;
}
.signOut span:hover{
 cursor: pointer;
}
.el-aside {
  background-color: #242730;
  color: #333;
  text-align: center;
  line-height: 200px;
}
.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  /*line-height: 160px;*/
}
.el-footer {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 60px;
}
</style>
